/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package org.colswe.solestac.view;

import java.util.List;
import javax.swing.JCheckBox;
import org.colswe.solestac.control.AdministrationControl;
import org.colswe.solestac.entity.ActualUser;
import org.colswe.solestac.VO.RoleVO;
import org.colswe.solestac.VO.UserVO;

/**
 *
 * @author Julian Peña
 * @version 1.0
 */
public class CRUDUser extends javax.swing.JPanel {
    
    private List<UserVO> users = null;
    int filas;
    private Object [][] table;
    private long ids[];
    private AdministrationControl search = new AdministrationControl();
    

    ActualUser actualUser = ActualUser.getInstance();
    
    /**
     * Creates new form CRUDUser
     */
    public CRUDUser() {
        
        filas = 0;
        table = new String[filas][5];
        initComponents();
        rolCB.removeAll();
        rolCB.addItem("");
        List<RoleVO> roles = search.getAllRoles();
        for(RoleVO rol: roles){
            rolCB.addItem(rol.getName());
        }
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        nombreTF = new javax.swing.JTextField();
        DcumentoTF = new javax.swing.JTextField();
        BuscarB = new javax.swing.JButton();
        nuevoB = new javax.swing.JButton();
        rolCB = new javax.swing.JComboBox();
        jSeparator1 = new javax.swing.JSeparator();
        jScrollPane1 = new javax.swing.JScrollPane();
        usersTable = new javax.swing.JTable();

        setBackground(new java.awt.Color(255, 255, 255));

        jLabel1.setFont(new java.awt.Font("Bitstream Vera Sans", 0, 16)); // NOI18N
        jLabel1.setText("Rol:");

        jLabel2.setFont(new java.awt.Font("Bitstream Vera Sans", 0, 16)); // NOI18N
        jLabel2.setText("Nombre");

        jLabel3.setFont(new java.awt.Font("Bitstream Vera Sans", 0, 16)); // NOI18N
        jLabel3.setText("Documento");

        BuscarB.setFont(new java.awt.Font("Bitstream Vera Sans", 0, 16)); // NOI18N
        BuscarB.setText("Buscar");
        BuscarB.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                BuscarBActionPerformed(evt);
            }
        });

        nuevoB.setFont(new java.awt.Font("Bitstream Vera Sans", 0, 16)); // NOI18N
        nuevoB.setText("Crear Nuevo");
        nuevoB.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                nuevoBActionPerformed(evt);
            }
        });

        usersTable.setFont(new java.awt.Font("Bitstream Vera Sans", 0, 16)); // NOI18N
        usersTable.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {

            },
            new String [] {
                "Username", "Documento", "Rol", "", ""
            }
        ) {
            boolean[] canEdit = new boolean [] {
                false, false, false, true, true
            };

            public boolean isCellEditable(int rowIndex, int columnIndex) {
                return canEdit [columnIndex];
            }
        });
        usersTable.setAutoResizeMode(javax.swing.JTable.AUTO_RESIZE_LAST_COLUMN);
        jScrollPane1.setViewportView(usersTable);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(30, 30, 30)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                                .addComponent(jLabel3, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                .addComponent(jLabel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                            .addComponent(jLabel2, javax.swing.GroupLayout.PREFERRED_SIZE, 101, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(nombreTF, javax.swing.GroupLayout.PREFERRED_SIZE, 160, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(DcumentoTF, javax.swing.GroupLayout.PREFERRED_SIZE, 160, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(rolCB, javax.swing.GroupLayout.PREFERRED_SIZE, 216, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGap(94, 94, 94)
                        .addComponent(nuevoB, javax.swing.GroupLayout.PREFERRED_SIZE, 146, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addComponent(jSeparator1, javax.swing.GroupLayout.PREFERRED_SIZE, 390, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 689, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(BuscarB, javax.swing.GroupLayout.PREFERRED_SIZE, 116, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(31, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(29, 29, 29)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(nombreTF, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jLabel2, javax.swing.GroupLayout.PREFERRED_SIZE, 28, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGap(9, 9, 9)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel3, javax.swing.GroupLayout.PREFERRED_SIZE, 28, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(DcumentoTF))
                        .addGap(10, 10, 10)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 28, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(rolCB, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGap(26, 26, 26)
                        .addComponent(BuscarB, javax.swing.GroupLayout.PREFERRED_SIZE, 29, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(jSeparator1, javax.swing.GroupLayout.PREFERRED_SIZE, 10, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(nuevoB)
                        .addGap(0, 0, Short.MAX_VALUE)))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 345, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(83, 83, 83))
        );
    }// </editor-fold>//GEN-END:initComponents

    private void nuevoBActionPerformed(java.awt.event.ActionEvent evt) {
        Principal principal = actualUser.getPrincipal();
        principal.newUser();
    }
    
    
    private void BuscarBActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_BuscarBActionPerformed
        // TODO add your handling code here:
        String username = nombreTF.getText();
        String document = DcumentoTF.getText();
        String role = rolCB.getSelectedItem().toString();
        
        users = search.search(username,document,role);
        filas = users.size();
        table = new Object[filas][5];
        ids = new long[filas];
        for(int i=0;i<filas;i++){
            for(int j=0;j<3;j++)
                    ids[i]=users.get(i).getId();
                    table[i][0] = users.get(i).getUsername();
                    table[i][1] = users.get(i).getDocument();
                    table[i][2] = users.get(i).getRole();
                    table[i][3] = "Modificar";
                    table[i][4] = "Eliminar";
            }
        
        usersTable.setModel(new javax.swing.table.DefaultTableModel(table, new String[]{"Username","Documento", "Rol","Modificar","Eliminar"}));
        usersTable.getColumn("Modificar").setCellRenderer(new ButtonRenderer());
        usersTable.getColumn("Modificar").setCellEditor(new ButtonEditor(new JCheckBox(),usersTable,ids,table));
        usersTable.getColumn("Eliminar").setCellRenderer(new ButtonRenderer());
        usersTable.getColumn("Eliminar").setCellEditor(new ButtonDelete(new JCheckBox(),usersTable,ids,table));
    }//GEN-LAST:event_BuscarBActionPerformed

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton BuscarB;
    private javax.swing.JTextField DcumentoTF;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JSeparator jSeparator1;
    private javax.swing.JTextField nombreTF;
    private javax.swing.JButton nuevoB;
    private javax.swing.JComboBox rolCB;
    private javax.swing.JTable usersTable;
    // End of variables declaration//GEN-END:variables

}
